/**
 * Created by gongxun on 16/12/28.
 */

var KwxTableMaimaLayer = GameCCSBase.extend({
    Text_title : null,
    Panel_cards : null,
    mCards : null,
    mMaimaConfig : null,
    mMaimaZhongNums : null,

    Panel_single : null,
    Panel_six : null,
    ctor : function(){
        this._super();
        this.loadCocostudio("res/ccs/KwxTableMaima.json");

        this.setVisible(false);

        this.loadUI();
    },

    loadUI : function(){
        this.Text_title = mo.seekWidgetByName(this.ccsNode, "Text_title");
        this.Panel_cards = mo.seekWidgetByName(this.ccsNode, "Panel_cards");

        this.Panel_single = mo.seekWidgetByName(this.ccsNode, "Panel_single");
        this.Panel_six = mo.seekWidgetByName(this.ccsNode, "Panel_six");
    },

    loadCards : function(){
        if (this.mCards == null){
            this.mCards = [];
            for (var i = 0; i < 6; i++){
                var card = new KwxCard();
                this.Panel_cards.addChild(card);
                this.mCards.push(card);
            }
        }

        for (var i = 0; i < 6; i++){
            this.mCards[i].stopAllActions();
            this.mCards[i].setTag(i);
            this.mCards[i].setVisible(false);
        }
    },

    startMaimaAction : function(roundEndInfo){
        this.loadCards();

        var isMaima = false;
        var maimaNums = [];
        var maimaBeishu = 0;
        var winCard = 0;
        for (var seat = 0; seat < 3; seat++) {
            var logicSeat = mo.kwxTableStatus.getLogicForView(seat, roundEndInfo.MySeat - 1);
            var seatUser = roundEndInfo["SeatUser" + (logicSeat + 1)];
            if (seatUser == null){
                continue;
            }

            if (seatUser.Horses.length > 0){
                isMaima = true;
                maimaNums = seatUser.Horses;
                maimaBeishu = seatUser.HorseRate;
                winCard = seatUser.WinCard;

                // var maStr = "买马中:" + seatUser.HorseRate + "倍";
                // mo.gameHelper.setTextString(Text_maima, maStr);
                //
                // //加载精灵
                // var xOffset = 0;
                // var cardWidth = 28;
                // for (var i = 0; i < seatUser.Horses.length; i++){
                //     var cardNum = seatUser.Horses[i];
                //     var card = new KwxCard();
                //     card.setCardNum(cardNum);
                //     card.setCardKind(0, 3);
                //     var scale = cardWidth / card.getContentSize().width;
                //     card.setPosition(cc.p(xOffset, 0));
                //     card.setScale(scale);
                //     Panel_maima.addChild(card);
                //
                //     xOffset += cardWidth;
                // }
                //
                // break;
            }
        }

        var ruleConfig = mo.kwxTable.mTableConfig;
        var endTime = 3;
        if (isMaima){
            //背景动画
            this.playAnim(ruleConfig.horse == 1);

            this.mMaimaConfig = JSON.parse(roundEndInfo.CfgJson).horse;
            this.setMaimaBeishu(-1);

            this.mMaimaZhongNums = mo.kwxCardHelper.getMaimaZhong(maimaNums, winCard);
            this.setMaimaCards(maimaNums, maimaBeishu);

            //0.5秒后出现 1秒后执行动画 动画完毕后 2.5秒后出现结算框
            endTime = 0.5 + 1 + maimaNums.length * 0.2 + 2.5;
            this.stopAllActions();
            var that = this;
            this.runAction(cc.sequence(
                cc.delayTime(0.5),
                cc.show(),
                cc.delayTime(1 + maimaNums.length * 0.2 + 2.5),
                cc.callFunc(function(){
                    that.hideMaima();
                })))
        }else{
            this.setVisible(false);
        }

        return endTime;
    },

    //-1 显示初始
    setMaimaBeishu : function(maimaBeishu){
        if (maimaBeishu == -1){
            var tishi = this.mMaimaConfig == 1 ? "买独马" : "买六马";
            mo.gameHelper.setTextString(this.Text_title, tishi);
        }else{
            var tishi = this.mMaimaConfig == 1 ? "买独马" : "买六马";
            tishi += (" 中" + maimaBeishu + "倍");
            mo.gameHelper.setTextString(this.Text_title, tishi);
        }
    },

    setMaimaCards : function(maimaNums, maimaBeishu){
        var cardScale = 90 / mo.kwxCardHelper.inhandCardSize.height;
        var cardSize = cc.size(mo.kwxCardHelper.inhandCardSize.width * cardScale, mo.kwxCardHelper.inhandCardSize.height * cardScale);
        var offset = 15;
        var startPX = -((maimaNums.length - 1) / 2) * (cardSize.width + offset);

        for (var i = 0; i < maimaNums.length; i++){
            this.mCards[i].setVisible(true);

            this.mCards[i].setCardNum(maimaNums[i]);
            this.mCards[i].setCardKind(0, 4);
            this.mCards[i].setScale(cardScale);
            this.mCards[i].resetCardColor();

            var p = cc.p(startPX + (cardSize.width + offset) * i, 0);
            p = cc.p(p.x, p.y - cardSize.height / 2);
            this.mCards[i].setPosition(p);
        }

        var that = this;
        for (var i = 0; i < maimaNums.length; i++) {
            this.mCards[i].runAction(cc.sequence(
                cc.delayTime(0.5 + 1 + 0.2 * i),
                cc.callFunc(function(){
                    var card = this;
                    var index = card.getTag();
                    card.setCardNum(maimaNums[index]);
                    card.setCardKind(0, 3);
                    card.setScale(cardScale);
                    if (mo.gameHelper.findValueForArray(that.mMaimaZhongNums, maimaNums[index]) == null){
                        card.setCardColor(cc.color(128, 128, 128));
                    }

                    if (index == maimaNums.length - 1){
                        that.setMaimaBeishu(maimaBeishu);
                        mo.seekWidgetByName(that.Panel_single, "Image_singBg1").stopAllActions();
                        mo.soundHelper.playMjMaima();
                    }
                }, this.mCards[i])
            ));
        }
    },

    playAnim : function(isSing){
        if (isSing){
            this.Panel_single.setVisible(true);
            this.Panel_six.setVisible(false);

            //不转了
            // mo.seekWidgetByName(this.Panel_single, "Image_singBg0").stopAllActions();
            // mo.seekWidgetByName(this.Panel_single, "Image_singBg0").runAction(cc.repeatForever(
            //     cc.sequence(
            //         cc.rotateBy(6, 720)
            //     )
            // ));
            //
            // mo.seekWidgetByName(this.Panel_single, "Image_singBg1").stopAllActions();
            // mo.seekWidgetByName(this.Panel_single, "Image_singBg1").runAction(cc.repeatForever(
            //     cc.sequence(
            //         cc.rotateBy(4, 720)
            //     )
            // ));

        }else{
            this.Panel_single.setVisible(false);
            this.Panel_six.setVisible(true);

            mo.seekWidgetByName(this.Panel_six, "Image_sixBg1").stopAllActions();
            mo.seekWidgetByName(this.Panel_six, "Image_sixBg1").setOpacity(0);
            mo.seekWidgetByName(this.Panel_six, "Image_sixBg1").runAction(cc.repeatForever(
                cc.sequence(
                    cc.delayTime(0.5),
                    cc.fadeIn(0.5),
                    cc.delayTime(0.5),
                    cc.fadeOut(0.5)
                )
            ));


            mo.seekWidgetByName(this.Panel_six, "Image_sixLight0").stopAllActions();
            mo.seekWidgetByName(this.Panel_six, "Image_sixLight0").setVisible(true);
            mo.seekWidgetByName(this.Panel_six, "Image_sixLight0").runAction(cc.repeatForever(
                cc.sequence(
                    cc.delayTime(0.5),
                    cc.hide(),
                    cc.delayTime(0.5),
                    cc.show()
                )
            ));

            mo.seekWidgetByName(this.Panel_six, "Image_sixLight1").stopAllActions();
            mo.seekWidgetByName(this.Panel_six, "Image_sixLight1").setVisible(false);
            mo.seekWidgetByName(this.Panel_six, "Image_sixLight1").runAction(cc.repeatForever(
                cc.sequence(
                    cc.delayTime(0.5),
                    cc.show(),
                    cc.delayTime(0.5),
                    cc.hide()
                )
            ));
        }
    },

    hideMaima : function(){
        this.setVisible(false);

        this.stopMaimaAnim();
    },

    stopMaimaAnim : function(){
        //单马
        mo.seekWidgetByName(this.Panel_single, "Image_singBg0").stopAllActions();
        mo.seekWidgetByName(this.Panel_single, "Image_singBg1").stopAllActions();

        //双马
        mo.seekWidgetByName(this.Panel_six, "Image_sixBg1").stopAllActions();
        mo.seekWidgetByName(this.Panel_six, "Image_sixLight0").stopAllActions();
        mo.seekWidgetByName(this.Panel_six, "Image_sixLight1").stopAllActions();
    }
});